/**
 * 请求加载对象与设置
 */
let fetchLoading = null;
const fetchLoadingOptions = {
    text: '请求中', 
    spinner: 'el-icon-loading', 
    background: 'rgba(0, 0, 0, 0.5)'
};

/**
 * 全局混合
 */
const mixinPage = {
    data() {
        return {

            /**
             * 表单
             */
            form: {
                labelWidth: '120px',
            },

            /**
             * 列表
             */
            list: {
                loading: true,
                list: []
            },

            /**
             * 分页
             */
            paging: {
                loading: true,
                listOrigin: [],
                list: [],
                current: 1,
                size: 20,
                total: 0
            },

            /**
             * 弹出窗
             */
            dialog: {
                show1: false,
                show2: false,
                show3: false,
                show4: false,
                show5: false,
            },

            /**
             * 管道
             */
            pipe: {
                empty: {
                    start(obj) { },
                    end(obj) { },
                    error(res, obj) { },
                    fail(res, obj) { },
                    offline(err) { }
                },
                loading: {
                    start: (obj)=> { fetchLoading = this.$loading(fetchLoadingOptions); },
                    end: (obj)=> { fetchLoading.close() },
                    error: (res, obj)=> { },
                    fail: (res, obj)=> { },
                    offline: (err)=> { }
                },
                toast: {
                    start(obj) { },
                    end(obj) { },
                    error: (res, obj)=> { this.$message.warning(res.msg + '！') },
                    fail: (res, obj)=> { this.$message.error('服务器出错!') },
                    offline: (err)=> { this.$message.error('您没有联网!') }
                },
                loadingToast: {
                    start: (obj)=> { fetchLoading = this.$loading(fetchLoadingOptions); },
                    end: (obj)=> { fetchLoading.close() },
                    error: (res, obj)=> { this.$message.warning(res.msg + '！') },
                    fail: (res, obj)=> { this.$message.error('服务器出错!') },
                    offline: (err)=> { this.$message.error('您没有联网!') }
                }
            }
        }
        
    },
    mounted() {
        var app = document.getElementById('app');
        app.className = 'fadein'
    },
    methods: {

        /**
         * 跳转页面
         * 
         * @param {String} url 需要跳转的url地址
         */
        nav(url) {
            location.href = url;
        },

        /**
         * 返回上一页
         */
        back() {
            history.goback();
        },

        /**
         * 普通get请求
         */
        get(action, data, pipe, success) {
            http.get({
                url: location.pathname,
                data: Object.assign({action}, data),
                pipe,
                success
            });
        },

        /**
         * 普通post请求
         */
        post(action, data, pipe, success) {
            http.post({
                url: location.pathname + '?action=' + action,
                data,
                pipe,
                success
            });
        },

        /**
         * 列表快速加载
         * 
         * @param {Array} data 加载回的数据
         */
        listLoad(data) {
            this.list.list = data;
            this.list.loading = false;
        },

        /**
         * 分页快速加载
         * 
         * @param {Array} data 加载回的数据
         */
        pagingLoad(data) {
            this.paging.listOrigin = data;
            this.paging.list = this.pagingSliceArray(data, this.paging.current, this.paging.size);
            this.paging.total = data.length;
            this.paging.loading = false;
        },

        /**
         * 前端分页，当前页被改变
         * 
         * @param {Int} page 当前页码
         */
        pagingChangeCurrent(page) {
            this.paging.loading = true;
            this.paging.current = page;
            this.paging.list = this.pagingSliceArray(this.paging.listOrigin, page, this.paging.size);
            this.paging.loading = false;
        },

        /**
         * 前端分页快速获取数据
         * 
         * @param {Array} data 原始数据
         * @param {Int} current 当前页码
         * @param {Int} size 每页显示的条数
         */
        pagingSliceArray(data, current, size) {
            const start = (current - 1) * size;
            const end = start + size;
            return data.slice(start, end);
        },
        
        /**
         * 快速显示dialog
         * 
         * @param {Int} num dialog编号
         */
        dialogShow(num = 1) {
            switch (num) {
                case 1:
                this.dialog.show1 = true;
                break;
                case 2:
                this.dialog.show2 = true;
                break;
                case 3:
                this.dialog.show3 = true;
                break;
                case 4:
                this.dialog.show4 = true;
                break;
                case 5:
                this.dialog.show5 = true;
                break;
                default: 
                break;
            }
        },

        /**
         * 快速隐藏dialog
         * 
         * @param {Int} num dialog编号
         */
        dialogHide(num = 1) {
            switch (num) {
                case 1:
                this.dialog.show1 = false;
                break;
                case 2:
                this.dialog.show2 = false;
                break;
                case 3:
                this.dialog.show3 = false;
                break;
                case 4:
                this.dialog.show4 = false;
                break;
                case 5:
                this.dialog.show5 = false;
                break;
                default: 
                break;
            }
        },

        /**
         * 删除确认
         * 
         * @param {Function} call 回调方法
         */
        confirmDeleteItem(call) {
            this.$confirm('此操作将删除该条目, 是否继续?', '删除提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                call();
            }).catch(() => {
                      
            });
        }
        
    }
};
